/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.devplat.lmoroney.locationlesson2_3; import android.content.Intent; import android.location.Geocoder; import android.location.Location; import android.os.Handler; import android.os.ResultReceiver; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationServices; public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { protected static final String TAG = "main-activity"; protected static final String ADDRESS_REQUESTED_KEY = "address-request-pending"; protected static final String LOCATION_ADDRESS_KEY = "location-address"; private AddressResultReceiver mResultReceiver; protected GoogleApiClient mGoogleApiClient; protected Location mLastLocation; protected String mAddressOutput; protected TextView mLocationAddressTextView; Button mFetchAddressButton; protected synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); } @Override public void onConnectionFailed(ConnectionResult result) { Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); } public void onDisconnected() { Log.i(TAG, "Disconnected"); } @Override public void onConnectionSuspended(int cause) { Log.i(TAG, "Connection suspended"); mGoogleApiClient.connect(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mResultReceiver = new AddressResultReceiver(new Handler()); mLocationAddressTextView = (TextView) findViewById(R.id.location_address_view); mFetchAddressButton = (Button) findViewById(R.id.fetch_address_button); mAddressOutput = ""; buildGoogleApiClient(); } @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } @Override protected void onStop() { super.onStop(); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } public void fetchAddressButtonHandler(View view) { // We only start the service to fetch the address if GoogleApiClient is connected. if (mGoogleApiClient.isConnected() && mLastLocation != null) { startIntentService(); } } @Override public void onConnected(Bundle connectionHint) { // Gets the best and most recent location currently available, which may be null // in rare cases when a location is not available. mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if (mLastLocation != null) { // Determine whether a Geocoder is available. startIntentService(); } } protected void startIntentService() { Intent intent = new Intent(this, FetchAddressIntentService.class); intent.putExtra(Constants.RECEIVER, mResultReceiver); intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLastLocation); startService(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } protected void displayAddressOutput() { mLocationAddressTextView.setText(mAddressOutput); } class AddressResultReceiver extends ResultReceiver { public AddressResultReceiver(Handler handler) { super(handler); } /** * Receives data sent from FetchAddressIntentService and updates the UI in MainActivity. */ @Override protected void onReceiveResult(int resultCode, Bundle resultData) { // Display the address string or an error message sent from the intent service. mAddressOutput = resultData.getString(Constants.RESULT_DATA_KEY); displayAddressOutput(); } } }